package cn.bob.org.controller;

import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Dept;
import cn.bob.org.domain.User;
import cn.bob.org.query.UserQuery;
import cn.bob.org.service.UserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author bob
 * @data 2022/10/2-13:16
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;
    //查询所有数据
    @GetMapping
    public List<User> findAll(){
        List<User> all = service.findUserAll();
        return all;
    }
    //根据id查询的数据
    @GetMapping("/{id}")
    public User findById(@PathVariable("id") Long id){
        return service.findByUserId(id);
    }

    //根据部门查询的user数据
    @GetMapping("/deptuser/{id}")
    public  List<User> findByDeptId(@PathVariable("id") Long id){
        return service.findByDeptUserId(id);
    }
    //添加修改数据
    @PutMapping
    public AjaxResult addOrUp(@RequestBody User user){
        try {
        if (null==user.getUserId()){
            service.addUser(user);
        }else {
            service.updateUser(user);
        }
        return AjaxResult.me().success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
    //按照id删除数据
    @DeleteMapping("/{id}")
    public  AjaxResult delByUser(@PathVariable("id") Long id){
        try {
            service.delByUser(id);
            return AjaxResult.me().success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
    //批量删除
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            service.batchDel(ids);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }


    //分页查询
    @PostMapping
    public PageList<User> queryByUserPage(@RequestBody UserQuery query){
        return  service.queryByUserPage(query);
    }

}
